import { searchGpts } from "@/api/gpts"
import { getUser, mockGPTS } from "../api/third3"


const wrapperPromise = (promise) => {

    let status = 'pending'
    let result = null
    let suspender = promise.then(res => {
        status = 'success'
        result = res
    }, err => {
        status = 'error'
        result = err
    })

    return {
        read() {
            if('pending' === status) {
                // 注意 是抛出异常throw
                throw suspender
            }

            if('error' === status) {
                throw result
            }

            return result
        }
    }
}


// 创建各种资源
export const suspendResource = () => {
    return {
        userResource: wrapperPromise(getUser()),
        mockGptResource: wrapperPromise(mockGPTS()),
    }
}



export const searchGptsResource = (question) => {
    return wrapperPromise(searchGpts(question))
}
